class Solution(object):
    def maxWidthRamp(self, nums):
        st = [0]
        n = len(nums)
        max_len = 0

        def search(st, value):
            l = 0
            r = len(st) - 1
            while l < r:
                mid = (l + r) // 2
                if nums[st[mid]] <= value:
                    r = mid
                else:
                    l = mid + 1
            return st[l]

        for i in range(1, n):
            if nums[i] < nums[st[-1]]:
                st.append(i)
            else:
                start = search(st, nums[i])
                max_len = max(max_len, i - start)

        return max_len


data = Solution()
nums = [6,0,8,2,1,5]
print(data.maxWidthRamp(nums))
nums = [9, 8, 1, 0, 1, 9, 4, 0, 4, 1]
print(data.maxWidthRamp(nums))